<template>
	<view>
		<view class="login-container">
			<uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
			<!-- <button type="primary" class="btn-login" @click="getuserinfo">一键登录</button> -->
			<button type="primary" class="btn-login" @click="getuserinfo2">一键登录</button>
			<text class="tips-text">登录后尽享更多权益</text>
		</view>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapState
	} from 'vuex'
	export default {
		name: "sun-login",
		data() {
			return {
				// userInfo: []
			};
		},
		computed: {
			...mapState('m_user', ['userinfo'])
		},
		methods: {
			...mapMutations('m_user', ['updateUserInfo']),
			getuserinfo(e) {
				let that = this
				uni.login({
					success(res) {
						console.log(res.code);
						// that.getOpenId(res.code)
						// that.js_code = res.code
					}
				})
			},
			async getuserinfo2(e) {
				let that = this
				uni.getUserProfile({
					desc: '您的信息仅作为个人展示噢',
					success: (res) => {
						console.log('获取用户信息成功', res.userInfo)
						//获取用户信息的各类操作
						this.userInfo = res.userInfo
						uni.login({
							success(res) {
								that.userInfo['js_code'] = res.code
								that.getOpenId(that.userInfo)
								
							}
						})
						
					},
					fail: (res) => {
						console.log('获取用户信息失败', res)
						uni.showToast({
							title: '信息授权失败~',
							duration: 1000,
							icon: 'error',
							mask: true
						})
					}
				})
				
			},
			async getOpenId(userinfo) {
				const {data: res} = await uni.$http.post('/users/', userinfo)
				if(res.code !== '0') return uni.$showMsg('登录失败！')
				this.updateUserInfo(res.success)
				uni.$showMsg('登录成功')
				console.log(res);
			}
		}
	}
</script>

<style lang="scss">
.login-container {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		height: 750rpx;
		background-color: #f8f8f8;
		position: relative;
		overflow: hidden;

		&::after {
			content: '';
			display: block;
			width: 100%;
			height: 40px;
			background-color: white;
			position: absolute;
			bottom: 0;
			left: 0;
			border-radius: 100%;
			transform: translateY(50%);
		}

		.btn-login {
			width: 90%;
			border-radius: 100px;
			margin: 15px 0;
			background-image: linear-gradient(120deg, #89f7fe 0%, #66a6ff 100%);
		}

		.tips-text {
			font-size: 12px;
			color: gray;
		}
	}
</style>
